﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Siser.Web.Models;
using Siser.Web.Models.JsonResponses;
using Siser.Web.Shared;

namespace Siser.Web.Controllers
{ 
    public class RegionController : Controller
    {
        private SiserEntities db = new SiserEntities();

        public ViewResult Index()
        {
            return View(db.Regiones.ToList());
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        #region "   CRUD"

        public ActionResult Create()
        {
            return View();
        } 

        [HttpPost]
        public ActionResult Create(Region region)
        {
            if (ModelState.IsValid)
            {
                db.Regiones.Add(region);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(region);
        }
        
        public ActionResult Edit(int id)
        {
            Region region = db.Regiones.Find(id);
            return View(region);
        }

        [HttpPost]
        public ActionResult Edit(Region region)
        {
            if (ModelState.IsValid)
            {
                db.Entry(region).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(region);
        }

        public ViewResult Details(int id)
        {
            Region region = db.Regiones.Find(id);
            return View(region);
        }

        public ActionResult Delete(int id)
        {
            Region region = db.Regiones.Find(id);
            return View(region);
        }

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Region region = db.Regiones.Find(id);
            db.Regiones.Remove(region);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        #endregion

        [HttpPost]
        public JsonResult GetRegiones()
        {
            var regionesJson = (from r in db.Regiones
                                join x in
                                    (from e in db.Equipos
                                     join ue in db.UsuariosEquipos on e.Id equals ue.Equipo.Id
                                     join u in db.Usuarios on ue.Usuario.Id equals u.Id
                                     join i in db.Instalaciones on e.Instalacion.Id equals i.Id
                                     join p in db.Plantas on i.Planta.Id equals p.Id
                                     where e.Vigencia == true
                                        && u.Vigencia == true
                                        && u.Nombre == HttpContext.User.Identity.Name
                                     select p) on r.Id equals x.RegionId
                                select new JsonDataMap()
                                {
                                    Id = r.Id,
                                    Latitud = r.Latitud,
                                    Longitud = r.Longitud,
                                    Nombre = r.Numero + " Región: " + r.Nombre,
                                    Tipo = "REG",
                                    Controller = "",
                                    Action = "",
                                    Parametro = 0,
                                    NivelAlarma = ""
                                }).Distinct().ToList();

            for (int i = 0; i < regionesJson.Count; i++)
            {
                regionesJson[i].NivelAlarma = SystemAlarm.GetAlarmLevelByRegion(HttpContext.User.Identity.Name, regionesJson[i].Id);
                regionesJson[i].Id = null;
            }
            
            return Json(regionesJson);
        }

    }
}